using UnityEngine;
using UnityEngine.UI;

public enum DayOrNight
{
    Day,
    Night
}
public enum PlayerWeapon
{
    PrimaryWeapon,
    UpgradeWeapon
}
public class GameManager : Singleton<GameManager>
{
    [SerializeField] private Text text_DayCount;
    [SerializeField] private Text text_DayOrNight;
    public PlayerWeapon playerWeapon = PlayerWeapon.PrimaryWeapon;

    public GameObject playerPrefab;
    [SerializeField] private float hp;
    public float GetHp { get { return hp; } }
    public float SetHp { set { hp = value; } }
    [SerializeField] private float strength;
    public float GetStrength { get { return strength; } }
    public float SetStrength { set { strength = value; } }
    [SerializeField] private float strengthDecreaseRate;
    public Slider hpBar;
    public Slider strengthBar;
    public int dayCount = 1;
    public DayOrNight dayNightEnum = DayOrNight.Day;


    public bool isBattling;

    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }

    private void Update()
    {
        hpBar.value = hp / 100;
        strengthBar.value = strength / 100;
        if (isBattling)
        {
            HandlePlayerProperty();
        }
    }

    public void UpdateDayCountAndDayState()
    {
        if(dayNightEnum == DayOrNight.Night)
        {
            dayCount = dayCount + 1;
            dayNightEnum = DayOrNight.Day;
            UpdateUIDayNight();
            return;
        }
        dayNightEnum = DayOrNight.Night;
        UpdateUIDayNight();
    }

    public void IncreasePlayerStrength(float value)
    {
        strength += value;
        if(strength >= 100)
        {
            strength = 100;
        }
    }

    private void UpdateUIDayNight()
    {
        text_DayCount.text = "第 " + dayCount + " 天";
        if (dayNightEnum == DayOrNight.Day)
        {
            text_DayOrNight.text = "白天";
        }
        if (dayNightEnum == DayOrNight.Night)
        {
            text_DayOrNight.text = "夜晚";
        }
    }


    private void HandlePlayerProperty()
    {
        strength -= strengthDecreaseRate * Time.deltaTime;
        strength = Mathf.Clamp(strength, 0, 100);
        if (strength <= 0 || hp <= 0)
        {
            GameSceneManager.GetInstance().LoadSceneToHome();
            isBattling = false;
        }
    }

}
